// Learn cc.Class:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/class.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/class.html
// Learn Attribute:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/life-cycle-callbacks.html

const MJTool = require('MJCommon');
const tool = require('ToolUtil');
const CardSpriteType = MJTool.CardSpriteType;
const STARTHUTYPE = MJTool.STARTHUTYPE;
const EatCardType = MJTool.EatCardType;
const HUTYPE = MJTool.HUTYPE;
cc.Class({
    extends: cc.Component,
    // mPlayer: null, ///对就的玩家
    // m_getCard: 0, //刚抓的牌
    // mShowHuCard: 0, //是不是可以显示胡的牌
    // mHuitemList: null,
    // m_cpgSpriteList: null,
    // m_nCPGCount: 0, //吃碰杠的数量 
    // m_EatCards: null,
    ctor() {
        // this.m_getCard = 0; //刚抓的牌
        // this.mShowHuCard = 0; //是不是可以显示胡的牌
        // this.m_nCPGCount = 0;
        // this.m_EatCards = [{}, {}, {}, {}];
        // this.m_cpgSpriteList = [];
    },



    init: function () {
        this.m_getCard = 0; //刚抓的牌
        this.mShowHuCard = 0; //是不是可以显示胡的牌
        this.m_nCPGCount = 0;
        this.m_EatCards = [{}, {}, {}, {}];
        this.m_cpgSpriteList = [];
        this.m_lastOutCards = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];

    },

    finish: function () {
        this.m_getCard = undefined;
        this.mShowHuCard = undefined;
        this.m_nCPGCount = undefined;
        this.m_EatCards = undefined;
        this.m_cpgSpriteList = undefined;
        this.m_lastOutCards = undefined;
    },

    gameStart: function () {
        this.mQuick = false;
        this.m_cardSpriteList = [];
        this.m_outCardSpriteList = [];
        this.m_cpgSpriteList = [];
        this.m_mTingCard = [];
        this.m_birdNums = 0;
        this.m_nCPGCount = 0;
        this.m_nCardNums = 0;
        this.mShowHuCard = 0;
        this.m_nOutCount = 0;
        this.m_boReady = false;
        this.m_boMingPai = false;
        this.mHuitemList = [];
        this.m_nLastOutCardCount = 0;
        this.m_nFirstHu = STARTHUTYPE.none;
        this.m_boHandDown = false;
        this.m_boTing = false;
        this.m_getCard = 0;
        this.hideTimer();
        for (var i = 0; i < 18; i++) {
            this.m_nCards[i] = 0xf0;
            this.m_lastOutCards[i] = 0;
        }
        for (var i = 0; i < 40; i++) {
            this.m_nOutCards[i] = 0;
        }

        for (var i = 0; i < 4; i++) {
            this.m_EatCards[i] = {
                type: 0,
                card: 0,
                first: 0
            };
        }

        var scnPly = cc.find('Canvas').getComponent('MJScene');
        if (scnPly == null) {
            return;
        }

        if (this.m_nSeadOrd >= 0 && this.m_nSeadOrd <= 4) {
            scnPly.gameFace[this.m_nSeadOrd].setIsReady(false);
            scnPly.gameFace[this.m_nSeadOrd].setQuickState(false);
        }
    },

    /**
     * 显示胡的牌
     */
    showHintHu: function () {
        if (this.m_mTingCard && this.m_mTingCard.length > 0) {
            for (var i = 0; i < this.m_mTingCard.length; i++) {
                var card = this.m_mTingCard[i].card;
                for (var l = 0; l < this.m_cardSpriteList.length; l++) {
                    if (this.m_cardSpriteList[l].node.active && this.m_cardSpriteList[l].getCard() == card) {
                        this.m_cardSpriteList[l].showHuHint();
                    }
                }
            }
        }
    },

    removeCardFromHand: function (card) {
        if (this.mShowHuCard != 0) {
            var idx = this.m_lastOutCards.indexOf(card);
            if (idx >= 0) {
                this.m_nLastOutCardCount--;
                this.m_lastOutCards[idx] = this.m_lastOutCards[this.m_nLastOutCardCount];
                this.m_lastOutCards[this.m_nLastOutCardCount] = 0;
                this.m_nCardNums--;
                if (this.m_boMingPai) {
                    idx = this.m_nCards.indexOf(card);
                    this.m_nCards[idx] = this.m_nCards[this.m_nCardNums];
                    this.m_nCards[this.m_nCardNums] = 0;
                }
                return true;
            } else if (!this.m_boMingPai) {
                this.m_nCardNums--;
            }
        }
        var idx = this.m_nCards.indexOf(card);
        if (idx >= 0) {
            this.m_nCards[idx] = this.m_nCards[this.m_nCardNums - 1];
            this.m_nCards[this.m_nCardNums] = 0;
            if (!this.m_boMingPai) {
                this.m_nCardNums--;
            } else if (idx < this.m_nCardNums) {
                this.m_nCardNums--;
            }
            return true;
        }
        return false;
    },

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {},

    getCpgStartPos: function (map) {
        var startPos = null;
        startPos = cc.pAdd(map.mCPG.startPos, cc.pMult(map.mCPG.fix3, this.m_nCPGCount));
        startPos = cc.pAdd(startPos, cc.pMult(map.mCPG.fix, (this.m_nCPGCount * 3 - 1)));
        if (map.mCPG.fix.x == 0) {
            startPos.x = map.mHand.startPos.x;
            startPos.y += map.mCPG.fix.y;
            if ((this.m_boHandDown) || (this.m_nSeadOrd == 1 || this.m_nSeadOrd == 3)) {
                startPos.x = map.mShow.startPos.x;
            }
        } else if (map.mCPG.fix.y == 0) {
            startPos.y = map.mHand.startPos.y;
            startPos.x += map.mCPG.fix.x;
            if ((this.m_boHandDown) || (this.m_nSeadOrd == 1 || this.m_nSeadOrd == 3)) {
                startPos.y = map.mShow.startPos.y;
            }
        }
        return startPos;
    },

    /**
     * 生成手牌
     * @param {*} noSort 
     */
    makeCardSprites: function (noSort) {
        var scene = cc.find('Canvas').getComponent('MJScene');
        var mjData = scene.MJCARD[this.m_nSeadOrd];

        if (this.m_boMingPai) {
            scene.clearTouchSpriteList();
        }

        for (var i = 0; i < this.m_cardSpriteList.length; i++) {
            this.m_cardSpriteList[i].node.active = false;
            this.m_cardSpriteList[i].node.destroy();
        }


        this.m_cardSpriteList = [];

        if (noSort == null) {
            noSort = false;
        }

        if (this.m_boMingPai && !noSort && this.m_nCardNums > 0) {
            MJTool.sortCard(this.m_nCards, this.m_nCardNums);
        }

        if (this.mShowHuCard != 0) {
            scene.clearTouchSpriteList();
            this.mActionFun.makeCardWithStartHu();
        } else {

            var fix = scene.MJCARD[this.m_nSeadOrd].mHand.fix;
            var startPos = scene.MJCARD[this.m_nSeadOrd].mHand.startPos;
            if ((this.m_boHandDown) || (this.m_boMingPai)) {
                startPos = scene.MJCARD[this.m_nSeadOrd].mShow.startPos;
                fix = scene.MJCARD[this.m_nSeadOrd].mShow.fix;
            }
            if (this.m_nCPGCount > 0) {
                startPos = this.mActionFun.getCpgStartPos.call(this, scene.MJCARD[this.m_nSeadOrd]);
            }

            var zIndex = 200;
            for (var i = 0; i < this.m_nCardNums; i++) {
                if (this.m_nCards[i] == 0) continue;
                var card = this.m_nCards[i];
                if (!this.m_boMingPai) card = 0xf0;
                var spr = cc.instantiate(scene.MJSprite);
                if (spr) {
                    if (this.m_boHandDown) {
                        spr.getComponent('MJCardSprite').init(card, this.m_nSeadOrd, mjData, CardSpriteType.cstHandShow);
                    } else {
                        if (card != 0xf0 && (this.m_nSeadOrd == 1 || this.m_nSeadOrd == 3)) {
                            spr.getComponent('MJCardSprite').init(card, this.m_nSeadOrd, mjData, CardSpriteType.cstHandShow);
                        } else {
                            spr.getComponent('MJCardSprite').init(card, this.m_nSeadOrd, mjData, CardSpriteType.cstHandDown);
                        }
                    }
                    if (this.m_nSeadOrd == 1) {
                        zIndex--;
                    }
                    this.m_cardSpriteList.push(spr.getComponent('MJCardSprite'));
                    spr.setPosition(cc.pAdd(startPos, cc.pMult(fix, i)));
                    if ((13 - this.m_nCPGCount * 3) == i) {
                        spr.setPosition(cc.pAdd(spr.getPosition(), cc.pMult(fix, 0.1)));
                    }
                    spr.zIndex = zIndex;
                    scene.mMjCardLayer.addChild(spr);
                }
            }
        }
    },

    makeCPGSprites: function () {
        var scene = cc.find('Canvas').getComponent('MJScene');
        var mjData = scene.MJCARD[this.m_nSeadOrd];

        // var scnPly = PlayerManager.getGameScene();
        // if (scnPly == null)
        //     return;
        if (this.m_cpgSpriteList) {
            this.m_cpgSpriteList.forEach(function (item) {
                item.active = false;
            }, this);
        }
        this.m_cpgSpriteList = [];
        var cpgCards = [-1, -1, -1, -1];

        var startPos = cc.p(scene.MJCARD[this.m_nSeadOrd].mCPG.startPos.x, scene.MJCARD[this.m_nSeadOrd].mCPG.startPos.y);
        var fix = scene.MJCARD[this.m_nSeadOrd].mCPG.fix;
        var fix2 = scene.MJCARD[this.m_nSeadOrd].mCPG.fix2;

        var zIndex = 30;
        if (this.m_nSeadOrd == 3) {
            zIndex = 0;
        } else if (this.m_nSeadOrd == 1) {
            zIndex = 250;
        }

        for (var i = 0; i < this.m_nCPGCount; i++) {
            var item = this.m_EatCards[i];
            MJTool.createCpgItem(cpgCards, item);
            startPos = cc.pAdd(scene.MJCARD[this.m_nSeadOrd].mCPG.startPos, cc.pMult(fix, 3 * i));
            startPos = cc.pAdd(startPos, cc.pMult(scene.MJCARD[this.m_nSeadOrd].mCPG.fix3, i));
            for (var j = 0; j < 4; j++) {
                if (cpgCards[j] != -1) {
                    var spr = cc.instantiate(scene.MJSprite);
                    if (spr) {
                        spr.getComponent('MJCardSprite').init(cpgCards[j], this.m_nSeadOrd, mjData, CardSpriteType.cstCPG);
                    }
                    this.m_cpgSpriteList.push(spr);
                    scene.mMjCardLayer.addChild(spr);
                    if (this.m_nSeadOrd == 1) {
                        zIndex--;
                    }
                    if (j == 3) {
                        spr.setPosition(cc.pAdd(cc.pSub(startPos, fix), fix2));
                        spr.zIndex = zIndex + 3;
                    } else {
                        spr.setPosition(startPos);
                        startPos = cc.pAdd(startPos, fix);
                        spr.zIndex = zIndex;
                    }
                }
            }
        }

    },

    makeOutSprite: function () {
        var scene = cc.find('Canvas').getComponent('MJScene');
        var mjData = scene.MJCARD[this.m_nSeadOrd];
        // var scnPly = PlayerManager.getGameScene();
        // if (scnPly == null)
        //     return;
        scene.clearTouchSpriteList();
        this.clearOutCardSprites();
        var fix = scene.MJCARD[this.m_nSeadOrd].mOut.fix;
        var startPos = cc.p(scene.MJCARD[this.m_nSeadOrd].mOut.startPos.x, scene.MJCARD[this.m_nSeadOrd].mOut.startPos.y);
        var outColumn = scene.MJCARD[this.m_nSeadOrd].outColumn;
        var zIndex = 100;
        if (this.m_nSeadOrd == 0) {
            zIndex = 200;
        } else if (this.m_nSeadOrd == 2) {
            zIndex = 30;
        } else if (this.m_nSeadOrd == 3) {
            zIndex = 100;
        }
        for (var i = 0; i < this.m_nOutCount; i++) {
            var spr = cc.instantiate(scene.MJSprite);
            if (spr) {
                spr.getComponent('MJCardSprite').init(this.m_nOutCards[i], this.m_nSeadOrd, mjData, CardSpriteType.cstOutCard);
                this.m_outCardSpriteList.push(spr.getComponent('MJCardSprite'));
                spr.setPosition(startPos);
                startPos = cc.pAdd(startPos, fix);
                if (this.m_nSeadOrd == 2 || this.m_nSeadOrd == 3) {
                    zIndex++;
                } else {
                    zIndex--;
                }
                spr.zIndex = zIndex;
                scene.mMjCardLayer.addChild(spr);
                if ((i + 1) == outColumn) {
                    startPos = cc.pAdd(scene.MJCARD[this.m_nSeadOrd].mOut.startPos, scene.MJCARD[this.m_nSeadOrd].mOut.fix2);
                } else if ((i + 1) == outColumn * 2 - 2) {
                    startPos = cc.pAdd(scene.MJCARD[this.m_nSeadOrd].mOut.startPos, cc.pMult(scene.MJCARD[this.m_nSeadOrd].mOut.fix2, 2));
                } else if ((i + 1) == outColumn * 3 - 6) {
                    startPos = cc.pAdd(scene.MJCARD[this.m_nSeadOrd].mOut.startPos, cc.pMult(scene.MJCARD[this.m_nSeadOrd].mOut.fix2, 3));
                } else if ((i + 1) == outColumn * 4 - 12) {
                    startPos = cc.pAdd(scene.MJCARD[this.m_nSeadOrd].mOut.startPos, cc.pMult(scene.MJCARD[this.m_nSeadOrd].mOut.fix2, 4));
                } else if ((i + 1) == outColumn * 5 - 20) {
                    startPos = cc.pAdd(scene.MJCARD[this.m_nSeadOrd].mOut.startPos, cc.pMult(scene.MJCARD[this.m_nSeadOrd].mOut.fix2, 5));
                }
            }
        }
    },

    outCard: function (card) {
        var scnPly = cc.find('Canvas').getComponent('MJScene');
        if (this == tool.PlayerManager().m_MySelf && this.m_nCPGCount * 3 + this.m_nCardNums == 13) {
            tool.NetManager().stopForeverMsg();
            //判断最后一张牌是不是相等
            if (this.m_nOutCards[this.m_nOutCount - 1] != card) {
                tool.NetManager().refresh();
            }
            return;
        }

        if (scnPly._lastOutCardSprite) {
            scnPly._lastOutCardSprite.hideArrow();
            // scnPly._lastOutCardSprite.setColor(cc.color.WHITE);
            scnPly._lastOutCardSprite.hitToOut();
        }

        if (scnPly._lastOutCardSprite2) {
            scnPly._lastOutCardSprite2.hideArrow();
            // scnPly._lastOutCardSprite2.setColor(Color3B(255, 255, 255));
        }
        scnPly._lastOutCardSprite2 = scnPly._lastOutCardSprite;
        scnPly.cpgBar.active = false;

        // playEffect('send');

        // var sound = '';
        // switch (Number.parseInt(card / 10)) {
        //     case 0:
        //         sound = 'wan/'
        //         break;
        //     case 1:
        //         sound = 'tiao/'
        //         break;
        //     case 2:
        //         sound = 'tong/'
        //         break;
        // }
        // sound += Number.parseInt(card % 10);
        // this.playSound(sound);

        var mjSpr = null;
        if (this.m_boMingPai) {
            if (this.mShowHuCard != 0) {
                this.mActionFun.removeCardFromHand.call(this, card);
            } else {
                this.m_nCardNums--;
                var idx = this.m_nCards.indexOf(card); //去除相应的
                this.m_nCards[idx] = this.m_nCards[this.m_nCardNums];
            }
            var j = -1;
            for (var i = 0; i < this.m_cardSpriteList.length; i++) {
                if (this.m_cardSpriteList[i].getCard() == card) {
                    mjSpr = this.m_cardSpriteList[i];
                    j = i;
                    if (mjSpr.isChecked()) {
                        break;
                    }
                }
            }
            if (j != -1) {
                this.m_cardSpriteList.splice(j, 1);
            }

        } else {
            //this.m_nCardNums--;
            if (this.mShowHuCard != 0) {
                //是打的显示中的牌
                this.mActionFun.removeCardFromHand.call(this, card);
                this.makeCardSprites();
            } else {
                this.m_nCardNums--;
                if (this.m_cardSpriteList.length > this.m_nCardNums) {
                    mjSpr = this.m_cardSpriteList[this.m_cardSpriteList.length - 1];
                    this.m_cardSpriteList.splice(this.m_cardSpriteList.length - 1, 1);
                }

            }
        }


        // if (mjSpr) {
        //     // var p = mjSpr.node.getPosition();
        //     // mjSpr.setCard(card);
        //     // mjSpr.m_btDir = this.m_nSeadOrd; 
        //     // mjSpr._btType = CARDSPRITETYPE.cstHandCard;
        //     // mjSpr.setChecked(false);
        //     // mjSpr.updateSprite();
        //     // mjSpr.setPosition(p);
        // } else {
        //     mjSpr = new MJCardSprite(card, this.m_nSeadOrd, 14, CARDSPRITETYPE.cstHandCard, this);
        //     scnPly.mMjCardLayer.addChild(mjSpr);
        // }

        mjSpr.hitCard(this.m_nOutCount);
        mjSpr.showArrow(scnPly.arrowSpr);
        if (this.m_nSeadOrd == 2) {
            mjSpr.node.zIndex = 200 + this.m_nOutCount;
        }

        this.m_outCardSpriteList.push(mjSpr);
        this.m_nOutCards[this.m_nOutCount++] = card;

        scnPly._lastOutCardSprite = mjSpr;
        scnPly._lastOutCard2 = scnPly._lastOutCard;
        scnPly._lastOutCard = card;

        scnPly._lastPlayer = this;

        if (this == tool.PlayerManager().m_MySelf) {
            if (scnPly._selectCard != null && scnPly.cardSelect.active) {
                scnPly._selectCard = null;
                scnPly._selectIdx = -1;
                scnPly.cardSelect.active = false;
            }
        }

        if (this.m_boMingPai && this.m_nCardNums + this.m_nCPGCount * 3 == 13) {
            this.makeCardSprites();
        }

        if (scnPly._curPlayer) {
            scnPly._curPlayer.turnOff();
        }
    },

    getCard: function (card) {
        var scnPly = cc.find('Canvas').getComponent('MJScene');
        if (this.m_nSeadOrd < 0)
            return;

        var mjData = scnPly.MJCARD[this.m_nSeadOrd];
        this.m_getCard = card;
        scnPly.cpgBar.active = false;
        this.showTimer(15);

        if (scnPly._lastOutCardSprite) {
            scnPly._lastOutCardSprite.getComponent('MJCardSprite').hitToOut();
        }

        if (scnPly._lastPlayer != this && scnPly._lastPlayer) {
            scnPly._lastPlayer.turnOff();
        }

        if (this == tool.PlayerManager().m_MySelf && this.m_nCPGCount * 3 + this.m_nCardNums == 13) {
            //是自己的回合。并且需要判断出牌
            scnPly.setHintVisible(false);
        } else if (this != tool.PlayerManager().m_MySelf) {
            // if(((GamePlayer *)PlayerManager::Myself())->mSelectCard!=nullptr){
            //     ((GamePlayer *)PlayerManager::Myself())->mSelectCard->setChecked(false);
            //     ((GamePlayer *)PlayerManager::Myself())->mSelectCard = nullptr;
            // }
        }



        // //把抓的牌添加到手中

        var spr = cc.instantiate(scnPly.MJSprite);
        if (spr) {
            if (card > 0 && card <= 35 && this.m_boHandDown) {
                spr.getComponent('MJCardSprite').init(card, this.m_nSeadOrd, mjData, CardSpriteType.cstHandShow);
            } else {
                spr.getComponent('MJCardSprite').init(card, this.m_nSeadOrd, mjData, CardSpriteType.cstHandDown);
            }
            var fix = scnPly.MJCARD[this.m_nSeadOrd].mHand.fix;
            var startPos = scnPly.MJCARD[this.m_nSeadOrd].mHand.startPos;
            if ((this.m_boHandDown) || (card != 0xf0 && (this.m_nSeadOrd == 1 || this.m_nSeadOrd == 3))) {
                startPos = scnPly.MJCARD[this.m_nSeadOrd].mShow.startPos;
                fix = scnPly.MJCARD[this.m_nSeadOrd].mShow.fix;
            }
            if (this.m_nCPGCount > 0) {
                startPos = this.mActionFun.getCpgStartPos.call(this, scnPly.MJCARD[this.m_nSeadOrd]);
            }
            startPos = cc.pAdd(startPos, cc.pMult(fix, 13 - 3 * this.m_nCPGCount));
            spr.setPosition(startPos);
            spr.zIndex = 300;
            if (this.m_nSeadOrd == 1) {
                spr.zIndex = 180;
            }
        }
        this.m_cardSpriteList.push(spr.getComponent('MJCardSprite'));
        scnPly.mMjCardLayer.addChild(spr);
        this.m_nCards[this.m_nCardNums++] = card;

        if (tool.PlayerManager().m_MySelf == this) {
            this.mActionFun.showHintHu();
        }
        this.turnOn();

        // //更新剩余牌数量
        // scnPly.mCardNums--;
        // scnPly.updateCardNums();
    },

    CPG: function (item) {
        var scene = cc.find('Canvas').getComponent('MJScene');
        scene.cpgBar.active = false;


        var isClearCard = true;
        if (scene._lastPlayer) {
            scene._lastPlayer.turnOff();
        }

        var lastSpr = scene._lastOutCardSprite;
        if (lastSpr) {
            if (lastSpr.getCard() == item.card) {
                scene.clearTouchSpriteList();
                if (scene._lastPlayer) {
                    scene._lastPlayer.m_nOutCount--;
                    for (var i = scene._lastPlayer.m_outCardSpriteList.length - 1; i >= 0; i--) {
                        if (scene._lastPlayer.m_outCardSpriteList[i] == lastSpr) {
                            scene._lastPlayer.m_outCardSpriteList.splice(i, 1);
                            break;
                        }
                    }
                }
                lastSpr.node.removeFromParent();
                scene._lastOutCardSprite = null;
                isClearCard = false;
            }
        }

        var cpgIndex = 0;
        var boPengToGang = false;
        var animName = '';
        var soundName = '';


        switch (item.type) {
            case EatCardType.ectShun:
                {
                    soundName = 'eat';
                    animName = 'chi';

                    this.m_nCPGCount++;
                }
                break;
            case EatCardType.ectPeng:
                soundName = 'peng';
                animName = 'peng';
                this.m_nCPGCount++;
                break;
            case EatCardType.ectGang:
                soundName = 'gang';
                animName = 'gang';
                for (var i = 0; i < this.m_nCPGCount; i++) {
                    if (this.m_EatCards[i].type == EatCardType.ectPeng && this.m_EatCards[i].card == item.card) {
                        this.m_EatCards[i].type = EatCardType.ectGang;
                        boPengToGang = true;
                        this.m_nCPGCount--;
                        break;
                    }
                }
                this.m_nCPGCount++;
                break;
            case EatCardType.ectAnGang:
                soundName = 'gang';
                animName = 'gang';
                this.m_nCPGCount++;
                break;
            case EatCardType.ectGangShow:
                soundName = 'gang';
                animName = 'gang';
                for (var i = 0; i < this.m_nCPGCount; i++) {
                    if (this.m_EatCards[this.m_nCPGCount].type == EatCardType.ectAnGang) {
                        this.m_EatCards[i].type = EatCardType.ectGang;
                        this.m_EatCards[i].card = item.card;
                    }
                }
                isClearCard = false;
                break;
            case EatCardType.ectFangGang:
                soundName = 'gang';
                animName = 'gang';
                this.m_nCPGCount++;
                break;
            case EatCardType.ectBu:
                soundName = 'bu';
                animName = 'bu';
                for (var i = 0; i < this.m_nCPGCount; i++) {
                    if (this.m_EatCards[i].type == EatCardType.ectPeng && this.m_EatCards[i].card == item.card) {
                        this.m_EatCards[i].type = EatCardType.ectBu;
                        boPengToGang = true;
                        this.m_nCPGCount--;
                        isClearCard = false;
                        break;
                    }
                }
                this.m_nCPGCount++;
                break;
            case EatCardType.ectAnBu:
                soundName = 'bu';
                animName = 'bu';
                this.m_nCPGCount++;
                break;
            case EatCardType.ectFangBu:
                soundName = 'bu';
                animName = 'bu';
                this.m_nCPGCount++;
                break;
        }
        if (isClearCard && scnPly.m_lastPlayer) {
            scnPly.m_lastPlayer.removeOutCard(item.card);
        }

        if (!boPengToGang) {
            this.m_EatCards[this.m_nCPGCount - 1].type = item.type;
            this.m_EatCards[this.m_nCPGCount - 1].card = item.card;
            this.m_EatCards[this.m_nCPGCount - 1].first = item.first;
        }

        if (item.type != EatCardType.ectGangShow) {
            if (!boPengToGang) {
                cpgIndex = this.m_nCPGCount - 1;
            }
            if (this.m_boMingPai || this.mShowHuCard != 0) {
                //去除吃的牌，有可能是别人的

                switch (item.type) {
                    case EatCardType.ectShun:
                    case EatCardType.ectPeng:
                        {
                            var cards = [item.card, item.card];
                            if (item.type == EatCardType.ectShun) {
                                if (item.card == item.first) {
                                    cards[0] = item.first + 1;
                                    cards[1] = item.first + 2;
                                } else if (item.card == item.first + 1) {
                                    cards[0] = item.first;
                                    cards[1] = item.first + 2;
                                } else {
                                    cards[0] = item.first;
                                    cards[1] = item.first + 1;
                                }
                            }

                            this.mActionFun.removeCardFromHand.call(this, cards[0]);
                            this.mActionFun.removeCardFromHand.call(this, cards[1]);

                        }
                        break;
                    case EatCardType.ectAnGang:
                    case EatCardType.ectFangGang:
                    case EatCardType.ectFangBu:
                    case EatCardType.ectGang:
                    case EatCardType.ectBu:
                    case EatCardType.ectAnBu:
                        var card = item.card;
                        var moveNums = 0;
                        var flag = true;
                        while (flag) {
                            flag = this.mActionFun.removeCardFromHand.call(this, card);
                            moveNums++;
                            if (moveNums == 4) {
                                flag = false;
                            }
                        }

                        if (this != scnPly._curPlayer && !this.m_boMingPai) {
                            this.m_nCardNums++; //手牌增加
                        }
                        break;
                }


            } else {
                switch (item.type) {
                    case EatCardType.ectShun:
                    case EatCardType.ectPeng:
                        this.m_nCardNums -= 2;
                        break;
                    case EatCardType.ectGang:
                    case EatCardType.ectFangGang:
                    case EatCardType.ectFangBu:
                    case EatCardType.ectBu:
                        if (boPengToGang) {
                            this.m_nCardNums -= 1;
                            if (this.m_nCPGCount * 3 + this.m_nCardNums < 13) {
                                this.m_nCardNums++;
                            }
                        } else {
                            this.m_nCardNums -= 3;
                        }
                        break;
                    case EatCardType.ectAnGang:
                    case EatCardType.ectAnBu:
                        this.m_nCardNums -= 4;
                        break;
                }
            }
        }



        if (this.mShowHuCard != 0 && this.m_nCardNums + this.m_nCPGCount * 3 > 14) {
            this.m_nCardNums = 14 - this.m_nCPGCount * 3;
        }

        this.makeCPGSprites();
        if (item.type == EatCardType.ectGangShow) {

        } else {
            // if (animName != null && animName.length > 0) {
            //     var anim = new ArmatureView();
            //     scnPly.addChild(anim, 100);
            //     anim.playOnceWithClean('shouyingtianxia_1', animName);
            //     anim.setPosition(MjSpriteManager.mHit[this.m_nSeadOrd].mStartPos2);
            // }
            // this.playSound(soundName);
            this.makeCardSprites();
        }
        this.turnOn();
    },

    callHu: function (data) {
        // playEffect("carddown");
        var scene = cc.find('Canvas').getComponent('MJScene');
        scene.setHintVisible(false);
        scene.cpgBar.active = false;
        scene.balance.mBirdNums = 0;

        this.mShowHuCard = 0;
        var isClear = false;

        scene.clearTouchSpriteList();
        if (scene._lastOutCardSprite && scene._lastOutCardSprite.getCard() == data.huCard) {
            scene._lastOutCardSprite.node.active = false;
            isClear = true;
        }

        // var anim = new ArmatureView();
        // anim.setPosition(MjSpriteManager.mHit[this.m_nSeadOrd].mStartPos2);
        // scnPly.mMjCardLayer.addChild(anim, 2010);

        if ((data.type & HUTYPE.qiangganghu) == HUTYPE.qiangganghu) {
            //是抢杠的胡 去掉玩家中的杠的情况
            //找到当前的玩家
            //将其中手牌部分清空
            if (scene._curPlayer) {
                for (var i = scene._curPlayer.m_nCPGCount - 1; i >= 0; i--) {
                    if (scene._curPlayer.m_EatCards[i].card = data.huCard) {
                        scene._curPlayer.m_EatCards[i].type = EATCARDTYPE.ectPeng;
                        scene._curPlayer.makeCPGSprites();
                        break;
                    }
                }
            }
        }




        if ((data.type & HUTYPE.zimohu) == HUTYPE.zimohu) {
            // anim.playOnceWithClean('shouyingtianxia_1', 'zimo');
            // this.playSound('zimo');
            //查找两个 
            if (!isClear && ((data.type & HUTYPE.tianhu) != HUTYPE.tianhu) && this.m_outCardSpriteList.length > 2) {
                if (this.m_outCardSpriteList[this.m_outCardSpriteList.length - 2].getCard() == data.huCard) {
                    scene.clearTouchSpriteList();
                    this.m_outCardSpriteList[this.m_outCardSpriteList.length - 2].node.removeFromParent();
                    this.m_outCardSpriteList.splice(this.m_outCardSpriteList.length - 2, 1);
                }
            }

        } else {
            // anim.playOnceWithClean('shouyingtianxia_1', 'hu');
            // this.playSound('hu');
            if (!isClear) {
                if (scene._curPlayer && scene._curPlayer.m_outCardSpriteList.length > 2 && scene._curPlayer.m_outCardSpriteList[scene._curPlayer.m_outCardSpriteList.length - 2].getCard() == data.huCard) {

                    scene.clearTouchSpriteList();
                    scene._curPlayer.m_outCardSpriteList[scene._curPlayer.m_outCardSpriteList.length - 2].removeFromParent();
                    scene._curPlayer.m_outCardSpriteList.splice(scene._curPlayer.m_outCardSpriteList.length - 2, 1);
                }
            }
        }

        this.m_boHandDown = true;
        this.m_boMingPai = true;
        this.m_nCardNums = 0;
        var cardInfoArr = [-1, -1, -1, -1];
        for (var i = 0; i < data.huhands.length; i++) {
            MJTool.createCpgItem(cardInfoArr, data.huhands[i]);
            for (var l = 0; l < 4; l++) {
                if (cardInfoArr[l] > 0 && cardInfoArr[l] != 0xf0) {
                    this.m_nCards[this.m_nCardNums++] = cardInfoArr[l];
                }
            }
        }
        if (this.m_nCards[this.m_nCardNums - 1] != data.huCard) {
            if (this.m_nCards[this.m_nCardNums - 2] == data.huCard) {
                this.m_nCards[this.m_nCardNums - 2] = this.m_nCards[this.m_nCardNums - 1];
                this.m_nCards[this.m_nCardNums - 1] = data.huCard;
            } else if (this.m_nCards[this.m_nCardNums - 3] == data.huCard) {
                this.m_nCards[this.m_nCardNums - 3] = this.m_nCards[this.m_nCardNums - 1];
                this.m_nCards[this.m_nCardNums - 1] = data.huCard;
            }
        }
        if (data.cpg == null)
            this.m_nCPGCount = 0;
        else
            this.m_nCPGCount = data.cpg.length;
        for (var i = 0; i < this.m_nCPGCount; i++) {
            this.m_EatCards[i] = data.cpg[i];
        }
        this.makeCPGSprites();
        this.makeCardSprites(true);
    },

    test() {
        cc.log('test');
        if (CardSpriteType == null) {
            cc.log('CardSpriteType == null');
        }
        if (tool == null) {
            cc.log('tool == null');
        }
        // var mjSpr = this.m_cardSpriteList[0];
        // mjSpr.hitCard(1);
    },

    test1() {
        cc.log('test1');
        if (CardSpriteType == null) {
            cc.log('CardSpriteType == null');
        }
        if (tool == null) {
            cc.log('tool == null');
        }

        var mjSpr = tool.PlayerManager().m_MySelf.m_cardSpriteList[0];
        mjSpr.hitCard(1);
    }

    // update (dt) {},
});